package com.mzba.easy.weibo.ui;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.actionbarsherlock.app.SherlockFragment;
import com.mzba.easy.weibo.R;
import com.mzba.easy.weibo.db.StatusTimelineEntry;
import com.mzba.easy.weibo.db.StatusTimelineGson;
import com.mzba.easy.weibo.util.AppContext;
import com.mzba.easy.weibo.util.BasicUIEvent;
import com.mzba.easy.weibo.util.StringUtil;
import com.mzba.easy.weibo.util.UICore;
import com.mzba.easy.weibo.util.WeiboManager;
import com.mzba.easy.weibo.widget.CustomListView;

/**
 * 
 * @author 06peng
 *
 */
public class UserinfoTimeLineFragment extends SherlockFragment implements BasicUIEvent, Callback {
	
	private CustomListView listView;
	private Handler handler;
	private WeiboManager weiboManager;
	private List<StatusTimelineEntry> timelineEntries;
	private StatusTimeLineAdapter adapter;
	private int count = 20;
	private int page = 1;
	private String since_id = "0";
	private boolean firstLoad;
	
	private final int get_friends_timeline = 1;
	private final int refresh_timeline = 2;
	private final int get_more_data = 3;
	
	private boolean isLoading;
	
	private String userid;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		handler = new Handler(this);
		weiboManager = new WeiboManager(getActivity());
		userid = getArguments().getString("userid");
		UICore.eventTask(this, (UserinfoActivity) getActivity(), get_friends_timeline, null);
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.status_timeline_fragment, null);
		listView = (CustomListView) view.findViewById(R.id.status_timeline_scrollview);
		listView.setonRefreshListener(new CustomListView.OnRefreshListener() {
			
			@Override
			public void onRefresh() {
				menuRefresh();
			}
		});
		listView.setonLoadListener(new CustomListView.OnLoadListener() {
			
			@Override
			public void onLoad() {
				page++;
				UICore.eventTask(UserinfoTimeLineFragment.this, (UserinfoActivity) getActivity(), get_more_data, null);
			}
		});
		return view;
	}
	
	public void menuRefresh() {
		page = 1;
		UICore.eventTask(UserinfoTimeLineFragment.this, (UserinfoActivity) getActivity(), refresh_timeline, null);
	}

	@Override
	public void execute(int mes, Object obj) {
		try {
			switch (mes) {
			case get_friends_timeline:
				isLoading = false;
				if (StringUtil.isBlank(userid)) {
					userid = AppContext.getInstance().getUid();
				}
				StatusTimelineGson timeline1 = weiboManager.getUserTimeline(userid, count, page, since_id);
				timelineEntries = timeline1.getStatuses();
				if (timelineEntries != null && timelineEntries.size() > 0) {
					since_id = timelineEntries.get(0).getId();
				}
				handler.sendEmptyMessage(get_friends_timeline);
				break;
			case refresh_timeline:
				isLoading = false;
				if (StringUtil.isBlank(userid)) {
					userid = AppContext.getInstance().getUid();
				}
				StatusTimelineGson timeline2 = weiboManager.getUserTimeline(userid, count, page, since_id);
				List<StatusTimelineEntry> newTimelineEntries = timeline2.getStatuses();
				if (newTimelineEntries != null && !newTimelineEntries.isEmpty()) {
					List<StatusTimelineEntry> temp = new ArrayList<StatusTimelineEntry>(timelineEntries);
					timelineEntries.clear();
					timelineEntries.addAll(newTimelineEntries);
					timelineEntries.addAll(temp);
					since_id = timelineEntries.get(0).getId();
				}
				handler.sendEmptyMessage(get_friends_timeline);
				break;
			case get_more_data:
				if (StringUtil.isBlank(userid)) {
					userid = AppContext.getInstance().getUid();
				}
				StatusTimelineGson timeline3 = weiboManager.getUserTimeline(userid, count, page, "0");
				List<StatusTimelineEntry> moredatas = timeline3.getStatuses();
				if (moredatas != null && !moredatas.isEmpty()) {
					timelineEntries.addAll(moredatas);
					handler.sendEmptyMessage(get_more_data);
				}
				break;
			default:
				break;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public boolean handleMessage(Message msg) {
		switch (msg.what) {
		case get_friends_timeline:
			isLoading = true;
			firstLoad = true;
			if (timelineEntries != null && !timelineEntries.isEmpty()) {
				adapter = new StatusTimeLineAdapter(AppContext.getContext(), timelineEntries, listView);
				listView.setAdapter(adapter);
			}
			listView.onRefreshComplete();
			break;
		case get_more_data:
			if (adapter != null) {
				adapter.notifyDataSetChanged();
			}
			listView.setSelection(adapter.getCount() - 20);
			listView.onLoadComplete();
			break;
		default:
			break;
		}
		return false;
	}
	
	@Override
	public void onResume() {
		super.onResume();
		if (isLoading) {
			((UserinfoActivity) getActivity()).loadFinish();
		} else {
			((UserinfoActivity) getActivity()).loading();
		}
		if (firstLoad) {
			if (timelineEntries != null && !timelineEntries.isEmpty()) {
				adapter = new StatusTimeLineAdapter(AppContext.getContext(), timelineEntries, listView);
				listView.setAdapter(adapter);
			}
		}
	}
}
